package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.*;

public class Library {

    private LinkedHashMap<String, Book> Books = new LinkedHashMap<String, Book>();
    private LinkedHashMap<String, Movie> Movies = new LinkedHashMap<String, Movie>();
    private LinkedHashMap<String, User> Users = new LinkedHashMap<String, User>();


    public Library() {

        Books.put("001", new Book("Song Of Fire And Ice", "George R.R Martin"));
        Books.put("002", new Book("Persian Fire", "Tom Holland"));
        Books.put("003", new Book("Harry Potter And The Philosopher's Stone", "J.K Rowling"));
        Books.put("004", new Book("Harry Potter And The Goblet Of Fire", "J.K Rowling"));
        Books.put("005", new Book("Greatest Show on Earth", "Richard Dawkins"));

        Movies.put("pacificrim", new Movie("Pacific Rim", "2013", "Guillermo Del Toro", "*****"));
        Movies.put("ironman3", new Movie("Iron Man 3", "2013", "Shane Black", "***"));
        Movies.put("djangounchained", new Movie("Django Unchained", "2012", "Quentin Tarantino", "*****"));
        Movies.put("xmenwolverine", new Movie("X Men Wolverine", "2009", "Gavin Wood", "**"));
        Movies.put("300", new Movie("300", "2007", "Zack Snyder", "****"));
        Movies.put("manofsteel", new Movie("Man Of Steel", "2013", "Zack Snyder", "****"));
        Movies.put("batmanbegins", new Movie("Batman Begins", "2006", "Chris Nolan", "****"));
        Movies.put("darkknightrises", new Movie("The Dark Knight Rises", "2012", "Chris Nolan", "*****"));
        Movies.put("darkknight", new Movie("The Dark Knight", "2008", "Chris Nolan", "*****"));
        Movies.put("avatar", new Movie("Avatar", "2013", "James Cameron", "*****"));
        Movies.put("titanic", new Movie("Titanic", "1996", "James Cameron", "****"));
        Movies.put("tropicthunder", new Movie("Tropic Thunder", "2010", "Ben Stiller", "*****"));
        Movies.put("transformers", new Movie("Transformers", "2007", "Michael Bay", "*****"));
        Movies.put("matrix", new Movie("The Matrix", "2000", "Wachowski Brothers", "*****"));
        Movies.put("pulpfiction", new Movie("Pulp Fiction", "2000", "Quentin Tarantino", "*****"));


        Users.put("111-1111", new User("simba", "simba@gmail.com", "0823446558", "password1", false));
        Users.put("111-1223", new User("benny", "bez@hotmail.com", "0718089975", "password2", false));
        Users.put("112-3355", new User("sophy", "sophy@uct.ac.za", "0605573221", "password3", false));
    }


    public String listBooks() {
        String list = "";
        for (Map.Entry<String, Book> entry : Books.entrySet()) {
            list += entry.getKey().toString() + " " + entry.getValue().toString() + "\n";
        }
        return list;

    }

    public String listAMovie(String MovieID) {
        if (Movies.containsKey(MovieID)) {
            return (Movies.get(MovieID).toString());
        } else {
            return "Sorry, movie not found";
        }
    }

    public String reserveBook(String bookID) {

        if (Books.containsKey(bookID)) {
            return Books.get(bookID).reserveBook();

        } else {
            return "Sorry we don't have that book yet.";
        }
    }

    public boolean isValidUser(String username, String password) {
        return (Users.containsKey(username) && Users.containsValue(Users.get(username)) && (Users.get(username).validatePassword(password)));

    }

    public boolean aUserLoggedIn() {
        for (Map.Entry<String, User> entry : Users.entrySet()) {
            if (entry.getValue().isLoggedIn()) {

                return true;
            }
        }
        return false;
    }

    public User getLoggedInUser(String username) {
        return Users.get(username);
    }


}
